package com.wbcs.system.tags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.wbcs.config.Config;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.config.component.container.page.PageBean;
import com.wbcs.exception.WbcsRuntimeException;
import com.wbcs.system.inputbox.PopUpBox;

public class PopUpInputBoxTag extends TagSupport
{
    private static final long serialVersionUID=-7292339076480586578L;

    @Override
	public int doStartTag() throws JspException
    {
        HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
        JspWriter out=pageContext.getOut();
        String pageid=request.getParameter("SRC_PAGEID");
        String reportid=request.getParameter("SRC_REPORTID");
        String inputboxid=request.getParameter("INPUTBOXID");
        pageid=pageid==null?"":pageid.trim();
        inputboxid=inputboxid==null?"":inputboxid.trim();
        reportid=reportid==null?"":reportid.trim();
        PageBean pbean=Config.getInstance().getPageBean(pageid);
        
        if(pbean==null)
        {
            return EVAL_BODY_INCLUDE;
            //throw new WbcsRuntimeException("页面ID："+pageid+"对应的页面不存在");
        }
        ReportBean rbean=pbean.getReportChild(reportid,true);
        if(rbean==null)
        {
            throw new WbcsRuntimeException("ID为"+pageid+"的页面下不存在ID为"+rbean.getId()+"的报表");
        }
        
        String boxid=inputboxid;
//        String boxidSuffix=null;//输入框后缀，只对可编辑数据自动列表报表有效，即以__rowidx为后缀
        int idx=boxid.lastIndexOf("__");
        if(idx>0)
        {
            boxid=boxid.substring(0,idx);

        }
        PopUpBox popupboxObj=rbean.getPopUpBox(boxid);
        if(popupboxObj==null)
        {
            throw new JspException("报表"+rbean.getPath()+"下面不存在ID为"+boxid+"的弹出输入框");
        }
        try
        {
            out.println(popupboxObj.createSelectOkFunction(inputboxid));
        }catch(IOException e)
        {
            throw new WbcsRuntimeException("初始化报表"+rbean.getPath()+"的弹出输入框失败",e);
        }

        return EVAL_BODY_INCLUDE;
    }
}
